package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.MemberDao;
import com.itheima.dao.MenuDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Menu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService{
    @Autowired
    private MenuDao menuDao;

    @Override
    public List<Map<String, Object>> findMenuparentMenuId() {
        List<Map<String, Object>> list = new ArrayList<>();
        List<Menu> menus = menuDao.findMenuparentMenuId();
        for (Menu menu : menus) {
            if(menu.getLevel() != 1){
                continue;
            }
            Map<String, Object> map = new HashMap<>();
            map.put("value", menu.getId());
            map.put("label", menu.getName());
            List<Menu> childrenMenus = menu.getChildren();
            List<Map<String, Object>> childrenList = new ArrayList<>();
            if(childrenMenus != null && childrenMenus.size() > 0){
                for (Menu childrenMenu : childrenMenus) {
                    Map<String, Object> map1 = new HashMap<>();
                    map1.put("value", childrenMenu.getId());
                    map1.put("label", childrenMenu.getName());
                    childrenList.add(map1);
                }
            }
            map.put("children", childrenList);
            list.add(map);
        }
        return list;
    }

    @Override
    public void add(Menu menu) {
        String path = null;
        if(menu.getPriority() == null || "".equals(menu.getPriority())){
            Integer minPriority = menuDao.findMinPriorityByLevel(menu.getLevel());
            if(minPriority == null){
                menu.setPriority(1);
            }else{
                menu.setPriority(minPriority - 1);
            }
        }else {
            Integer id = menu.getPriority();
            Menu PriorityMenu = menuDao.findById(id);
            menu.setPriority(PriorityMenu.getPriority());
        }
        if(menu.getParentMenuId() != null){
            Menu parentMenu = menuDao.findFCMenuById(menu.getParentMenuId());
            String ParentPath = parentMenu.getPath();
            int count = parentMenu.getChildren().size();
            ++count;
            if(ParentPath.contains("/")){
                path = ParentPath + "-" + count;
            }else{
                path = "/" + ParentPath + "-" + count;
            }
            menu.setPath(path);
        }else{
            int count = menuDao.findCountByLevel(1);
            count = count + 2;
            path = count + "";
            menu.setPath(path);
        }
        menuDao.add(menu);
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        List<Menu> list = menuDao.findByCondition(queryPageBean.getQueryString());
        for (Menu menu : list) {
            Integer id = menu.getId();
            List<Menu> childrenList = menuDao.findByParentMenuId(id);
            menu.setChildren(childrenList);
        }
        PageInfo<Menu> pageInfo = new PageInfo<>(list);
        return new PageResult(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public Menu findById(Integer id) {
        return menuDao.findById(id);
    }

    @Override
    public List<Menu> findAll() {
        return menuDao.findAll();
    }

    @Override
    public List<Integer> findParentIdsByMenuId(Integer id) {
        List<Integer> list = new ArrayList<>();
        Integer parentId = menuDao.findParentIdById(id);
        if(parentId == null){
            return null;
        }
        list.add(parentId);
        Integer pparentId = menuDao.findParentIdById(parentId);
        if (pparentId != null){
            list.add(pparentId);
            Integer ppparentId = menuDao.findParentIdById(pparentId);
            if(ppparentId != null){
                list.add(ppparentId);
            }
        }
        return list;
    }

    @Override
    public List<Map<String, Object>> findMenuIdAndDisabled(Integer id) {
        List<Map<String, Object>> list = new ArrayList<>();
        List<Menu> menus = menuDao.findMenuIdAndDisabled(id);
        for (Menu menu : menus) {
            if(menu.getLevel() != 1){
                continue;
            }
            Map<String, Object> map = new HashMap<>();
            map.put("value", menu.getId());
            map.put("label", menu.getName());
            List<Menu> childrenMenus = menu.getChildren();
            List<Map<String, Object>> childrenList = new ArrayList<>();
            for (Menu childrenMenu : childrenMenus) {
                Map<String, Object> map1 = new HashMap<>();
                map1.put("value", childrenMenu.getId());
                map1.put("label", childrenMenu.getName());
                childrenList.add(map1);
            }
            map.put("children", childrenList);
            list.add(map);
        }
        return list;
    }

    @Override
    public void edit(Menu menu) {
        List<Menu> list = menuDao.findByParentMenuId(menu.getId());
        if(list.size() > 0){
            throw new RuntimeException("该菜单下关联其他菜单,请修改后重新编辑");
        }
        String path = null;
        if(menu.getPriority() == null || "".equals(menu.getPriority())){
            Integer minPriority = menuDao.findMinPriorityByLevel(menu.getLevel());
            if(minPriority == null){
                menu.setPriority(1);
            }else{
                menu.setPriority(minPriority - 1);
            }
        }else if (menu.getPriority() <= 0){

        }else {
            Integer id = menu.getPriority();
            Menu PriorityMenu = menuDao.findById(id);
            menu.setPriority(PriorityMenu.getPriority() + 1);
        }
        if(menu.getParentMenuId() != null){
            Menu parentMenu = menuDao.findFCMenuById(menu.getParentMenuId());
            String ParentPath = parentMenu.getPath();
            int count = parentMenu.getChildren().size();
            ++count;
            if(ParentPath.contains("/")){
                path = ParentPath + "-" + count;
            }else{
                path = "/" + ParentPath + "-" + count;
            }
            menu.setPath(path);
        }else{
            int count = menuDao.findCountByLevel(1);
            count = count + 2;
            path = count + "";
            menu.setPath(path);
        }
        menuDao.edit(menu);
    }

    @Override
    public void delete(Integer id) {
        List<Menu> list = menuDao.findByParentMenuId(id);
        if(list.size() > 0){
            throw new RuntimeException("该菜单下关联其他菜单,请删除后重试");
        }
        Integer count = menuDao.findRelationRoleCountByMenuId(id);
        if(count > 0){
            throw new RuntimeException("该菜单与角色相关联,请取消后重试");
        }
        menuDao.delete(id);
    }
}
